﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Corsat.Library.DataManager;

namespace Corsat.Web._2013.Administration.Results
{
    public partial class Technology : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                List<Library.Item> CourseNames = Corsat.Library.DataManager.Main.GetAllItemsByCategory(Library.DataManager.Category.TechnologyCourses);
                CourseNames.Insert(0, new Library.Item());
                ddlCourseNames.DataSource = CourseNames;
                ddlCourseNames.DataTextField = "Value";
                ddlCourseNames.DataValueField = "Id";
                ddlCourseNames.DataBind();

                DataTable dtAccounts = Main.GetAllAccountsWithCorsatId(DateTime.Now.Year);

                if (dtAccounts != null && Session["dtAccountsWithCorsatId"] == null)
                    Session["dtAccountsWithCorsatId"] = dtAccounts;


            }
        }

        protected void btnLoad_Click(object sender, EventArgs e)
        {

        }

        
        protected void lnkAddNew_Click(object sender, EventArgs e)
        {

        }

        protected void ddlCourseNames_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void txtCorsatId_TextChanged(object sender, EventArgs e)
        {
            DataTable dtAccounts = new DataTable();

            if (Session["dtAccountsWithCorsatId"] != null)
            {
                dtAccounts = (DataTable)Session["dtAccountsWithCorsatId"];

                DataRow[] drAccounts = dtAccounts.Select("CorsatId = " + txtCorsatId.Text);

                if (drAccounts.Length > 0)
                {
                    txtFullName.Text = drAccounts[0]["ArabicName"].ToString();
                    hfAccountId.Value = drAccounts[0]["AccountId"].ToString();
                }
            }

            this.programmaticModalPopup_AlreadyRegistered.Show();
        }

        protected void btnSaveTechResult_Click(object sender, EventArgs e)
        {
            try
            {
                int AccountId = Convert.ToInt32(hfAccountId.Value);
                int CourseId = Convert.ToInt32(ddlCourseNames.SelectedValue);
                float Result = (float)Convert.ToDouble(txtResult.Text);

                int GradeId = 0;

                if ((Result / 20) * 100 >= 85)
                    GradeId = 315;
                else if((Result / 20) * 100 >= 75)
                    GradeId = 314;
                else if ((Result / 20) * 100 >= 65)
                    GradeId = 313;
                else if ((Result / 20) * 100 >= 50)
                    GradeId = 312;
                else if ((Result / 20) * 100 < 50)
                    GradeId = 316;

/*                int Grade = 
310	ضعيف جدا
311	ضعيف
312	مقبول
313	جيد
314	جيد جدا
315	امتياز
316	له حق الإعادة
317	ناجح
 */
                Main.SaveTechnologyResult(CourseId, AccountId, Result, GradeId, calTestDate.SelectedDate);

                grdAccounts.DataSource = Main.GetTechnologyResults(CourseId, calTestDate.SelectedDate);
                grdAccounts.DataBind();

                txtCorsatId.Text = "";
                txtFullName.Text = "";
                txtResult.Text = "";
            }
            catch (Exception exc)
            { 
            
            }
        }

        protected void btnLoadAccount_Click(object sender, EventArgs e)
        {
            DataTable dtAccounts = new DataTable();

            if (Session["dtAccountsWithCorsatId"] != null)
            {
                dtAccounts = (DataTable)Session["dtAccountsWithCorsatId"];

                DataRow[] drAccounts = dtAccounts.Select("CorsatId = " + txtCorsatId.Text);

                if (drAccounts.Length > 0)
                    txtFullName.Text = drAccounts[0]["ArabicName"].ToString();
            }
        }

        protected void btnLoad_Click1(object sender, EventArgs e)
        {
            int CourseId = Convert.ToInt32(ddlCourseNames.SelectedValue);
            grdAccounts.DataSource = Main.GetTechnologyResults(CourseId, calTestDate.SelectedDate);
            grdAccounts.DataBind();
        }

    }
}